// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.figures;

import org.dengues.core.sql.SqlUtils;
import org.dengues.model.database.EMFDataType;
import org.dengues.model.database.ForeignKey;
import org.eclipse.draw2d.ArrowLocator;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.ConnectionLocator;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-17 qiang.zhang $
 * 
 */
public class ForeignKeyFigure extends PolylineConnection {

    private final ForeignKey foreignKey;

    /**
     * Qiang.Zhang.Adolf@gmail.com ForeignKeyFigure constructor comment.
     */
    public ForeignKeyFigure(ForeignKey foreignKey) {
        setForegroundColor(ColorConstants.darkGreen);
        if (foreignKey.getSource().getTable().getDbDiagram().getEmfType().equals(EMFDataType.MAPPER)) {
            setForegroundColor(ColorConstants.orange);
            setLineWidth(2);
        }
        this.foreignKey = foreignKey;
        updateJoinImage();
    }

    private org.eclipse.draw2d.PolylineDecoration createSourceDecoration() {
        org.eclipse.draw2d.PolylineDecoration df = new org.eclipse.draw2d.PolylineDecoration();

        org.eclipse.draw2d.geometry.PointList pl = new org.eclipse.draw2d.geometry.PointList();
        pl.addPoint(-1, 1);
        pl.addPoint(0, 0);
        pl.addPoint(-1, -1);
        df.setTemplate(pl);
        df.setScale(7, 3);

        return df;
    }

    private org.eclipse.draw2d.PolylineDecoration createTargetDecoration() {
        org.eclipse.draw2d.PolylineDecoration df = new org.eclipse.draw2d.PolylineDecoration();

        org.eclipse.draw2d.geometry.PointList pl = new org.eclipse.draw2d.geometry.PointList();
        pl.addPoint(-1, 1);
        pl.addPoint(0, 0);
        pl.addPoint(-1, -1);
        df.setTemplate(pl);
        df.setScale(7, 3);

        return df;
    }

    private IFigure createMiddleDecoration() {
        Object object = foreignKey.getProperties().get(SqlUtils.FK_JOIN);
        boolean b = object == null || SqlUtils.FK_NJOIN.equals(object);
        if (!b) {
            org.eclipse.draw2d.PolygonDecoration df = new org.eclipse.draw2d.PolygonDecoration();
            org.eclipse.draw2d.geometry.PointList pl = new org.eclipse.draw2d.geometry.PointList();
            if (SqlUtils.FK_LJOIN.equals(object)) {
                pl.addPoint(-2, 2);
                pl.addPoint(0, 0);
                pl.addPoint(-2, -2);
                pl.addPoint(-2, 2);
            } else if (SqlUtils.FK_RJOIN.equals(object)) {
                pl.addPoint(2, 2);
                pl.addPoint(0, 0);
                pl.addPoint(2, -2);
                pl.addPoint(2, 2);
            } else {
                pl.addPoint(-2, 2);
                pl.addPoint(2, -2);
                pl.addPoint(2, 2);
                pl.addPoint(-2, -2);
                pl.addPoint(-2, 2);
            }
            df.setTemplate(pl);
            df.setScale(7, 3);
            return df;
        } else {
            org.eclipse.draw2d.PolylineDecoration df = new org.eclipse.draw2d.PolylineDecoration();
            org.eclipse.draw2d.geometry.PointList pl = new org.eclipse.draw2d.geometry.PointList();
            pl.addPoint(-2, 2);
            pl.addPoint(0, 0);
            pl.addPoint(-2, -2);
            pl.addPoint(-2, 2);
            df.setTemplate(pl);
            df.setScale(7, 3);
            return df;
        }
    }

    public void updateJoinImage() {
        getChildren().clear();
        setTargetDecoration(createTargetDecoration());
        int value = foreignKey.getSource().getTable().getDbDiagram().getEmfType().getValue();
        if (value == EMFDataType.QUERY_VALUE) {
            add(createMiddleDecoration(), new ArrowLocator(this, ConnectionLocator.MIDDLE));
        }
    }

}
